package com.gitee.mars.marsquickframe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mars.marsquickframe.entity.SysUser;
import com.gitee.mars.marsquickframe.request.AddressBookRequest;
import com.gitee.mars.marsquickframe.response.base.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gitee.mars.marsquickframe.service.IAddressBookService;
import com.gitee.mars.marsquickframe.mapper.AddressBookMapper;
import com.gitee.mars.marsquickframe.entity.AddressBook;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.Objects;


/**
 * AddressBook 实现层
 *
 * @Author wq
 * @Date 2021-05-03
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements IAddressBookService {


    @Override
    public void add(AddressBookRequest request) {
        AddressBook addressBook = new AddressBook();
        addressBook.setName(request.getName());
        addressBook.setGender(request.getGender());
        addressBook.setCreateTime(new Date());
        addressBook.setRemark(request.getRemark());
        addressBook.setUpdateTime(new Date());
        addressBook.setMobile(request.getMobile());
        save(addressBook);
    }

    @Override
    public PageInfo<AddressBook> list(AddressBookRequest request) {
        IPage<AddressBook> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotEmpty(request.getName())) {
            wrapper.like(AddressBook::getName, request.getName());
        }
        IPage<AddressBook> sysUserIPage = baseMapper.selectPage(page, wrapper);
        if (!CollectionUtils.isEmpty(sysUserIPage.getRecords())) {
            return new PageInfo<>(page.getTotal(), sysUserIPage.getRecords());
        }
        return null;
    }

    @Override
    public void delete(Integer id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void update(AddressBookRequest request) {
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(request, addressBook);
        baseMapper.updateById(addressBook);
    }

    @Override
    public AddressBook detail(Integer id) {
        return baseMapper.selectById(id);
    }
}